package xuhaijia.lanou3g.autohome.recommend.newest;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.NetTool;
import xuhaijia.lanou3g.autohome.recommend.recommendreusing.RecommendBean;


/**
 * Created by dllo on 16/8/15.
 */
public class NewestAdapter extends BaseAdapter {
    private Context context;
    private RecommendBean arrayList;
    private int currentType;
    private static final int TYPE_COUNT = 3;
    private static final int TYPE_BIG_IMAGE = 1;
    private static final int TYPE_NORMAl = 0;

    public NewestAdapter(Context context) {
        this.context = context;
    }

    public void setArrayList(RecommendBean arrayList) {
        this.arrayList = arrayList;
        notifyDataSetChanged();
    }
    public void addArrayList(RecommendBean bean) {
        this.arrayList.getResult().getNewslist().addAll(bean.getResult().getNewslist());
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (arrayList.getResult().getNewslist() == null) {
            return 0;
        }
        return arrayList.getResult().getNewslist().size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.getResult().getNewslist().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {

        if (arrayList.getResult().getNewslist().get(position).getMediatype() == 6) {
            return TYPE_BIG_IMAGE;
        } else {
            return TYPE_NORMAl;
        }
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NetTool mNetTool = new NetTool();
        View normalView = null;
        View bigImageView = null;
        currentType = getItemViewType(position);
        RecommendBean.ResultBean.NewslistBean bean =
                arrayList.getResult().getNewslist().get(position);
        RecommendBean.ResultBean.HeadlineinfoBean headlineinfoBean =
                arrayList.getResult().getHeadlineinfo();
        if (currentType == TYPE_NORMAl) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                normalView = LayoutInflater.from(context).inflate(R.layout.item_listview, null);
                viewHolder = new ViewHolder(normalView);
                normalView.setTag(viewHolder);
                convertView = normalView;
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.titleTv.setText(bean.getTitle());
            viewHolder.dateTv.setText(bean.getTime());
            if (bean.getId() == headlineinfoBean.getId()) {
                viewHolder.headlineTv.setText("头条");
                viewHolder.headlineTv.setVisibility(View.VISIBLE);
            } else {
                viewHolder.headlineTv.setVisibility(View.INVISIBLE);
            }
            if (bean.getMediatype() == 1) {
                viewHolder.historyTv.setText(bean.getReplycount() + "评论");
                mNetTool.getImage(bean.getSmallpic(), viewHolder.iv);
                viewHolder.talkerTv.setText("");
                viewHolder.talkerTv.setBackgroundResource(0);
            }
            if (bean.getMediatype() == 2) {
                viewHolder.talkerTv.setText("说客");
                viewHolder.talkerTv.setBackgroundResource(R.drawable.shape_textview);
                viewHolder.historyTv.setText(bean.getReplycount() + "评论");
                mNetTool.getImage(bean.getSmallpic(), viewHolder.iv);
            }
            if (bean.getMediatype() == 3) {
                viewHolder.historyTv.setText(bean.getReplycount() + "播放");
                mNetTool.getImage(bean.getSmallpic(), viewHolder.iv);
                viewHolder.talkerTv.setText("");
                viewHolder.talkerTv.setBackgroundResource(0);
            }
            if (bean.getMediatype() == 4) {
                viewHolder.historyTv.setText(bean.getReplycount() + "浏览");
                mNetTool.getImage(bean.getSmallpic(), viewHolder.iv);
                viewHolder.talkerTv.setText("口碑");
                viewHolder.talkerTv.setBackgroundResource(0);
            }
            if (bean.getMediatype() == 5) {
                Picasso.with(context).load(bean.getSmallpic()).into(viewHolder.iv);
                viewHolder.historyTv.setText(bean.getReplycount() + "回帖");
                viewHolder.talkerTv.setText("");
                viewHolder.talkerTv.setBackgroundResource(0);
            }


        } else if (currentType == TYPE_BIG_IMAGE) {
            ViewHolderForSix viewHolderForSix = null;
            if (convertView == null) {
                bigImageView = LayoutInflater.from(context).inflate(R.layout.item_forsix, null);
                viewHolderForSix = new ViewHolderForSix(bigImageView);
                bigImageView.setTag(viewHolderForSix);
                convertView = bigImageView;
            } else {
                viewHolderForSix = (ViewHolderForSix) convertView.getTag();
            }
            viewHolderForSix.titleForSixTv.setText(bean.getTitle());
            viewHolderForSix.historyForSixTv.setText(bean.getReplycount() + "图片");
            viewHolderForSix.dateForSixTv.setText(bean.getTime());
            String urlString = bean.getIndexdetail();
            int start = urlString.indexOf("http");
            int end = urlString.indexOf(",");
            String url = urlString.substring(start, end);
            String other = urlString.substring(end + 1, urlString.length());
            int startSec = other.indexOf("http");
            int endSec = other.indexOf(",");
            String urlSec = other.substring(startSec, endSec);
            String urlThird = other.substring(endSec + 1, other.length());
            mNetTool.getImage(url, viewHolderForSix.firstIv);
            mNetTool.getImage(urlSec, viewHolderForSix.secondIv);
            mNetTool.getImage(urlThird, viewHolderForSix.thirdIv);
        }
        return convertView;
    }

    private class ViewHolder {
        ImageView iv;
        TextView titleTv;
        TextView talkerTv;
        TextView dateTv;
        TextView historyTv;
        TextView headlineTv;

        public ViewHolder(View view) {
            iv = (ImageView) view.findViewById(R.id.item_iv);
            titleTv = (TextView) view.findViewById(R.id.item_title);
            dateTv = (TextView) view.findViewById(R.id.item_date);
            talkerTv = (TextView) view.findViewById(R.id.item_talker);
            historyTv = (TextView) view.findViewById(R.id.item_history);
            headlineTv = (TextView) view.findViewById(R.id.item_headline);

        }
    }

    private class ViewHolderForSix {
        TextView titleForSixTv;
        ImageView firstIv;
        ImageView secondIv;
        ImageView thirdIv;
        TextView dateForSixTv;
        TextView historyForSixTv;

        public ViewHolderForSix(View view) {
            titleForSixTv = (TextView) view.findViewById(R.id.title_forsix);
            firstIv = (ImageView) view.findViewById(R.id.first_iv);
            secondIv = (ImageView) view.findViewById(R.id.second_iv);
            thirdIv = (ImageView) view.findViewById(R.id.third_iv);
            dateForSixTv = (TextView) view.findViewById(R.id.date_forsix);
            historyForSixTv = (TextView) view.findViewById(R.id.history_forsix);

        }
    }

}
